<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/5/16 0016
 * Time: 下午 14:36
 */

/**
 * excel类,解析excel
 * @param string $filePath
 * @param int $sheet
 * @return array|void
 * @throws \PHPExcel_Exception
 * @throws \PHPExcel_Reader_Exception
 */
function format_excel2array($filePath='',$sheet=0)
{
    vendor('Classes.PHPExcel');
    if (empty($filePath) or !file_exists($filePath)) {
        die('file not exists');
    }
    $PHPReader = new \PHPExcel_Reader_Excel2007();        //建立reader对象
    if (!$PHPReader->canRead($filePath)) {
        $PHPReader = new \PHPExcel_Reader_Excel5();
        if (!$PHPReader->canRead($filePath)) {
            echo 'no Excel';
            return;
        }
    }
    $PHPExcel = $PHPReader->load($filePath);        //建立excel对象
    $currentSheet = $PHPExcel->getSheet($sheet);        //**读取excel文件中的指定工作表*/
    $allColumn = $currentSheet->getHighestColumn();        //**取得最大的列号*/
    $allRow = $currentSheet->getHighestRow();        //**取得一共有多少行*/
    $data = array();
    for ($rowIndex = 1; $rowIndex <= $allRow; $rowIndex++) {        //循环读取每个单元格的内容。注意行从1开始，列从A开始
        for ($colIndex = 'A'; $colIndex <= $allColumn; $colIndex++) {
            $addr = $colIndex . $rowIndex;
            $cell = $currentSheet->getCell($addr)->getValue();
            if ($cell instanceof PHPExcel_RichText) { //富文本转换字符串
                $cell = $cell->__toString();
            }
            $data[$rowIndex][$colIndex] = $cell;
        }
    }
    return $data;
}

//CURL请求的函数http_request()
//通过https 中的get 或 post
function https_request($url, $data = null)
{
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    if (!empty($data)){
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    }
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($curl);
    curl_close($curl);
    return $output;
}

/**
 * 获取access_token
 * @param $corpid  企业号ID
 * @param $secret  应用ID
 * @return mixed
 */
function access_token($corpid,$secret){
    $token_name = $corpid . '_meeting_access_token';
    if(!$token = S($token_name)){
        $url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={$corpid}&corpsecret={$secret}";
        $res = json_decode(https_request($url),true);
        $token = $res['access_token'];
        S($token_name,$token,7100);
    }
    return $token;
}
/**
 * 针对php5.5及以上的版本对curl中@不支持的特别方法
 * @param $url
 * @param $strPOST
 * @return bool|mixed
 */
function http_post_media($url,$strPOST)
{
    $oCurl = curl_init ();
    curl_setopt ( $oCurl, CURLOPT_SAFE_UPLOAD, false);
    if (stripos ( $url, "https://" ) !== FALSE) {
        curl_setopt ( $oCurl, CURLOPT_SSL_VERIFYPEER, FALSE );
        curl_setopt ( $oCurl, CURLOPT_SSL_VERIFYHOST, false );
    }
    curl_setopt ( $oCurl, CURLOPT_URL, $url );
    curl_setopt ( $oCurl, CURLOPT_RETURNTRANSFER, 1 );
    curl_setopt ( $oCurl, CURLOPT_POST, true );
    curl_setopt ( $oCurl, CURLOPT_POSTFIELDS, $strPOST );
    $sContent = curl_exec ( $oCurl );
    $aStatus = curl_getinfo ( $oCurl );
    curl_close ( $oCurl );
    if (intval ( $aStatus ["http_code"] ) == 200) {
        return $sContent;
    } else {
        return false;
    }
}

/**
 * 获取jssdk api_ticket票据
 * @return bool|string
 */
function get_ticket(){
    $redis = linkredis();
    $api_ticket = $redis->get('dpa_api_ticket');
    if($api_ticket == false){
        $access_token = access_token();
        $jsapi_ticket_url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token={$access_token}&type=jsapi";
        $jsapi_ticket = json_decode(https_request($jsapi_ticket_url),true);
        $ticket = $jsapi_ticket['ticket'];
        $redis->set('dpa_api_ticket', $ticket);
        $redis->expire('dpa_api_ticket', 7100);
        return $ticket;
    }else{
        return $api_ticket;
    }
}